home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / utilities / shells / csh519s.lha / set.c < prev    next >
C/C++ Source or Header  |  1992-02-07  |  9KB  |  406 lines

  1.  
  2. /*
  3.  * SET.C
  4.  *
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  * Version 2.07M by Steve Drew 10-Sep-87
  8.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  9.  * Version 5.00L by Urban Mueller 17-Feb-91
  10.  *
  11.  */
  12.  
  13. #include "shell.h"
  14.  
  15. static char *prompt_string( char *str, char *t );
  16. static void set_sys_var( char *name, char *val );
  17.  
  18. #define MAXLEVELS (4+MAXSRC)
  19.  
  20. static ROOT _Mbase[MAXLEVELS], *Mbase[MAXLEVELS], *Topbase[MAXLEVELS];
  21.  
  22. void
  23. init_mbase(void)
  24. {
  25.     int  i;
  26.     char *c;
  27.  
  28.     for( i=0; i<MAXLEVELS; i++ )
  29.         Topbase[i]=Mbase[i]=&_Mbase[i];
  30.  
  31. #ifdef isalphanum
  32.     for( c=isalph+'0'; c<=isalph+'9'; *c++=1 ) ;
  33.     for( c=isalph+'A'; c<=isalph+'Z'; *c++=1 ) ;
  34.     for( c=isalph+'a'; c<=isalph+'z'; *c++=1 ) ;
  35.     isalph['_']=1;
  36. #endif
  37. }
  38.  
  39.  
  40.  
  41. void
  42. set_var( int level, char *name, char *str )
  43. {
  44.     char *val;
  45.  
  46.     if(!str) str="";
  47.     if(!(val=malloc(strlen(str)+1))) {
  48.         ierror(NULL,512);
  49.         return;
  50.     }
  51.     strcpy( val, str );
  52.  
  53.     set_var_mem( level, name, val );
  54. }
  55.  
  56.  
  57.  
  58. void
  59. set_var_mem( int level, char *name, char *str ) /* does not make a copy */
  60. {
  61.     NODE **first, *node;
  62.     ROOT *root, *nul=0;
  63.     int  local= level & LEVEL_LOCAL;
  64.     char *t, c;
  65.  
  66.     for( t=name; isalphanum(*t); t++ ) ;
  67.     c=*t; *t=0;
  68.  
  69.     level &=~ LEVEL_LOCAL;
  70.  
  71.     for( root= Mbase[level]; root; root= local? nul : root->parent ) {
  72.         first= & root->first[*name & MAXHASH-1];
  73.         for( node=*first; node; node=node->next ) {
  74.             if( !strcmp( node->name, name) ) {
  75.                 free( node->text );
  76.                 goto copy;
  77.             }
  78.         }
  79.     }
  80.  
  81.     if(!(node=malloc(sizeof(NODE) + strlen(name)))) {
  82.         ierror(NULL,512);
  83.         return;
  84.     }
  85.     node->next=*first;
  86.     *first=node;
  87.     strcpy( node->name, name );
  88.  
  89. copy:
  90.     node->text=str;
  91.     if( *name=='_' )
  92.         set_sys_var( name, node->text );
  93.  
  94.     *t=c;
  95. }
  96.  
  97. static char VarBuf[256];
  98.  
  99. void *
  100. get_var( int level, void *varname )
  101. {
  102.     NODE *node;
  103.     ROOT *root;
  104.     char *t, c, *res=NULL;
  105.     int  f=*(char *)varname & MAXHASH-1;
  106.  
  107.     for ( t= varname; (signed char)*t>0; t++ ) ;
  108.     c=*t; *t=0;
  109.  
  110.     for( root= Mbase[level]; root; root=root->parent )
  111.         for( node=root->first[f]; node; node=node->next )
  112.             if( !strcmp(node->name,varname) )
  113.                 { res=node->text; goto done; }
  114.  
  115.     if(level==LEVEL_SET&& *(char*)varname!='_'&& Getenv( varname, VarBuf, 256 ))
  116.         res=VarBuf;
  117.  
  118. done:
  119.     *t=c;
  120.     return res;
  121. }
  122.  
  123. void
  124. unset_level( int level )
  125. {
  126.     NODE *node;
  127.     int i;
  128.  
  129.     for( i=0; i<MAXHASH; i++ ) {
  130.         for( node=Mbase[level]->first[i]; node; node=node->next ) {
  131.             if( *node->name=='_' && level==LEVEL_SET ) {
  132.                 Mbase[level]->first[i]= node->next;
  133.                 set_sys_var( node->name, get_var( LEVEL_SET, node->name ));
  134.             }
  135.             Free ( node->text );
  136.             Free ( node );
  137.         }
  138.         Mbase[level]->first[i] = NULL;
  139.     }
  140. }
  141.  
  142. void
  143. unset_var( int level, char *name )
  144. {
  145.     ROOT *root;
  146.     NODE **first, *node, *prev;
  147.     char *t, c;
  148.     int  f=*name & MAXHASH-1;;
  149.  
  150.     for( t=name; isalphanum(*t); t++ ) ;
  151.     c=*t; *t=0;
  152.  
  153.     for( root= Mbase[level]; root; root=root->parent ) {
  154.         first= & root->first[f];
  155.         for( node=*first, prev=NULL; node; prev=node, node=node->next ) {
  156.             if( !strcmp( node->name, name) ) {
  157.                 if( prev ) prev->next=node->next; else *first=node->next;
  158.                 Free( node->text );
  159.                 Free( node );
  160.                 if( *name=='_' )
  161.                     set_sys_var( name, NULL );
  162.                 goto done;
  163.             }
  164.         }
  165.     }
  166. done:
  167.     *t=c;
  168. }
  169.  
  170.  
  171. void
  172. set_var_n( int level, char *name, char *str, int n )
  173. {
  174.     char c, len=strlen(str);
  175.  
  176.     if( n>len )
  177.         n=len;
  178.  
  179.     if( n>=0 ) {
  180.         c=str[n]; str[n]=0;
  181.         set_var( level, name, str );
  182.         str[n]=c;
  183.     } else 
  184.         set_var( level, name, "" );
  185. }
  186.  
  187. int
  188. do_unset_var( char *str, int level )
  189. {
  190.     int i;
  191.  
  192.     for (i = 1; i < ac; ++i)
  193.         unset_var (level, av[i]);
  194.     return 0;
  195. }
  196.  
  197. int
  198. do_set_var( char *command, int level )
  199. {
  200.     ROOT *root;
  201.     NODE *node;
  202.     char *str, *val;
  203.     int  i=2, j= *av[0]=='a' ? ' ' : 0xA0;
  204.  
  205.     if( ac>2 ) {
  206.         if( *av[i]=='=' && !av[i][1]) i++;
  207.         val= compile_av( av, *av[i] ? i : i+1, ac, j, 0 );
  208.         set_var_mem(level, av[1], val);
  209.     } else if( ac==2 ) {
  210.         if (str=get_var(level,av[1])) printf ("%-10s %s\n", av[1], str);
  211.     } else if( ac==1 ) {
  212.         if( level& LEVEL_LOCAL )
  213.             root= Mbase[ level&~LEVEL_LOCAL];
  214.         else 
  215.             root= Topbase[ level ];
  216.         for( i=0; i<MAXHASH && !breakcheck(); i++ )
  217.             for( node=root->first[i]; node && !dobreak(); node=node->next ) {
  218.                 printf ("%s%-10s %s\n", o_lolite, node->name, node->text);
  219.                 quickscroll();
  220.             }
  221.     }
  222.  
  223.     return 0;
  224. }
  225.  
  226.  
  227. extern char shellvers[];
  228. extern char shellctr[];
  229. extern long ExecTimer, ExecRC;
  230.  
  231. static char *
  232. prompt_string( char *str, char *t )
  233. {
  234.     struct DateStamp dss;
  235.     char *u,buf[10];
  236.     DateStamp(&dss);
  237.  
  238.     if( !str ) {
  239.         *t=0;
  240.         return t;
  241.     }
  242.  
  243.     while (*str) {
  244.         if (*str!='%') {
  245.             *t++=*str++;
  246.             continue;
  247.         }
  248.         str+=2;
  249.         switch( str[-1] ) {
  250.         case 'p': t+=sprintf(t,"%s", get_var(LEVEL_SET, "_cwd"));  break;
  251.         case 'm': t+=sprintf(t,"%d", AvailMem( 0 )/1024);          break;
  252.         case 't': t+=sprintf(t,"%s", next_word(dates(&dss,0)));    break;
  253.         case 'c': t+=sprintf(t,"%s", o_hilite);                    break;
  254.         case 'v': t+=sprintf(t,"%s", shellvers );                  break;
  255.         case 'n': t+=sprintf(t,"%s", get_var(LEVEL_SET,"_clinumber"));break;
  256.         case 'h': t+=sprintf(t,"%d", H_num);                       break;
  257.         case 'd':    sprintf(t,"%s", dates(&dss,0));if(u=index(t,' '))t=u;break;
  258.         case 'f': t+=sprintf(t,"%s", oneinfo(get_var(LEVEL_SET,v_cwd),4));break;
  259.         case 's': t+=sprintf(t,"%s", (u=Getenv(shellctr,buf,10)?buf:"?"));break;
  260.         case 'x': t+=sprintf(t,"%2d",ExecRC);                             break;
  261.         case 'r': t+=sprintf(t,"%d", (signed char)
  262.                                        Myprocess->pr_Task.tc_Node.ln_Pri);break;
  263.         case 'e': t+=sprintf(t,"%d:%02d.%02d",ExecTimer/6000,ExecTimer/100%60,
  264.                                              ExecTimer%100);              break;
  265.         default : *t++=str[-2]; *t++=str[-1]; break;
  266.         }
  267.     }
  268.     *t=0;
  269.     return t;
  270. }
  271.  
  272. void
  273. push_locals( ROOT *newroot )
  274. {
  275.     int i;
  276.     NODE **nodeptr;
  277.  
  278.     newroot->parent=Mbase[ LEVEL_SET ];
  279.     Mbase[ LEVEL_SET ]=newroot;
  280.  
  281.     nodeptr=newroot->first;
  282.     for( i=MAXHASH; i>0; --i )
  283.         *nodeptr++=NULL;
  284. }
  285.  
  286. void
  287. pop_locals( void )
  288. {
  289.     unset_level( LEVEL_SET );
  290.     Mbase[ LEVEL_SET ]=Mbase[ LEVEL_SET ]->parent;
  291. }
  292.  
  293. int
  294. do_local(void)
  295. {
  296.     int i;
  297.  
  298.     if( ac==1 )
  299.         do_set_var( "", LEVEL_SET | LEVEL_LOCAL);
  300.     else 
  301.         for( i=1; i<ac; i++ )
  302.             set_var( LEVEL_SET | LEVEL_LOCAL, av[i], "");
  303.     return 0;
  304. }
  305.  
  306.  
  307. char truetitle[200];
  308.  
  309. char o_rback[12]="rback";
  310. char o_hilite[24], o_lolite[8], *o_csh_qcd, o_kick20, o_nobreak;
  311. char o_minrows, o_scroll, o_nowindow, o_noraw, o_vt100, o_nofastscr;
  312. char o_bground, o_resident, o_internal, o_pipe[16]="T:", o_datefmt;
  313. char o_abbrev=1, o_insert=1, *o_every;
  314. long o_noreq, o_failat=20;
  315.  
  316. extern char trueprompt[100];
  317.  
  318. #define ISVAR(x) ( !strcmp( name, x ) )
  319.  
  320. static void
  321. set_sys_var( char *name, char *val )
  322. {
  323.     char *put, col;
  324.  
  325.     if     (ISVAR(v_debug   )) debug=     val!=NULL;
  326.     else if(ISVAR(v_nobreak )) o_nobreak= val!=NULL;
  327.     else if(ISVAR(v_insert  )) o_insert=  val!=NULL;
  328.     else if(ISVAR(v_every   )) o_every = val;
  329.     else if(ISVAR(v_failat  )) o_failat= val?atoi(val):20;
  330.     else if(ISVAR(v_rback   )) strcpy(o_rback,val?val:"rback");
  331.     else if(ISVAR(v_abbrev  )) o_abbrev=val?*val!='n':1;
  332.     else if(ISVAR(v_datefmt )) o_datefmt= val && !strcmp(val,"subst");
  333.     else if(ISVAR(v_qcd     )) o_csh_qcd= val;
  334.     else if(ISVAR(v_noreq   )) Myprocess->pr_WindowPtr=(APTR)(o_noreq=val?-1:0);
  335.     else if(ISVAR(v_hist    )) S_histlen= val ? atoi(val) : 0;
  336.     else if(ISVAR(v_titlebar)) update_sys_var( v_titlebar );
  337.     else if(ISVAR(v_pipe    )) appendslash(strcpy(o_pipe,val?val:"t:"));
  338.     else if(ISVAR(v_verbose )) {
  339.         Verbose=0;
  340.         if( val ) {
  341.             if( index(val,'s' )) Verbose|= VERBOSE_SOURCE;
  342.             if( index(val,'a' )) Verbose|= VERBOSE_ALIAS;
  343.             if( index(val,'h' )) Verbose|= VERBOSE_HILITE;
  344.         }
  345.     } else if( ISVAR(v_hilite)) {
  346.         o_hilite[0]=o_lolite[0]=0;
  347.         if( !val )
  348.             val="";
  349.         put= o_hilite;
  350.         while( val && *val ) {
  351.             switch( *val++ ) {
  352.             case 'b': put+=sprintf( put, "\033[1m" ); break;
  353.             case 'i': put+=sprintf( put, "\033[3m" ); break;
  354.             case 'u': put+=sprintf( put, "\033[4m" ); break;
  355.             case 'r': put+=sprintf( put, "\033[7m" ); break;
  356.             case 'c': put+=strlen(put);
  357.                       if( *val>='0' && *val<='9' ) {
  358.                          col=*val++;
  359.                          if( *val==',' && val[1]>='0' && val[1]<='9' ) {
  360.                              put+=sprintf( put,"\033[3%cm\033[4%cm",col,val[1]);
  361.                              val+=2;
  362.                          } else 
  363.                              put+=sprintf( put,"\033[3%cm",col );
  364.                       }
  365.                       break;
  366.             }
  367.         }
  368.         *put=0;
  369.         if( *o_hilite )
  370.             strcpy(o_lolite,"\033[m");
  371.         strcpy(prompt_string(put,trueprompt),o_lolite);
  372.     } else if( ISVAR( v_scroll )) {
  373.         o_scroll=0;
  374.         if( val ) {
  375.             o_scroll=atoi( val );
  376.             if( o_scroll<2 ) o_scroll=0;
  377.             if( o_scroll>8 ) o_scroll=8;
  378.         }
  379.     } else if( ISVAR(v_minrows)) {
  380.         o_minrows=34;
  381.         if( val ) {
  382.             o_minrows=atoi( val );
  383.             if( o_minrows<8 )   o_minrows=8;
  384.             if( o_minrows>100 ) o_minrows=100, o_scroll=0;
  385.         }
  386.     }
  387. }
  388.  
  389. void
  390. update_sys_var( char *name )
  391. {
  392.     char c=name[1], *str, buf[250];
  393.  
  394.     if( c==v_prompt[1] ) {
  395.         if( (str=get_var(LEVEL_SET,v_prompt) ) ==NULL) str="$ ";
  396.         strcpy(prompt_string(str,trueprompt),o_lolite);
  397.     }
  398.     if( c==v_titlebar[1] && !o_nowindow && Win ) {
  399.         prompt_string( get_var(LEVEL_SET, v_titlebar), buf);
  400.         if (strcmp((char*)Win->Title, buf)) {
  401.             strcpy(truetitle,buf);
  402.             SetWindowTitles(Win, truetitle, (char *)-1);
  403.         }
  404.     }
  405. }
  406.